package vtt.android.docbaovn.database;

import java.util.ArrayList;
import java.util.List;

import vtt.android.library.utils.VttLog;
import vtt.android.docbaovn.BaseApplication;
import vtt.android.docbaovn.entities.Category;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CategoryDAO {

	VttLog log = new VttLog("CategoryDAO", BaseApplication.isDebuggable);
	// field database
	public static String TABLE_NAME = "category";

	public static final String COL_TYPE = "type";
	public static final String COL_NAME = "name";
	public static final String COL_ACTIVE = "active";
	
	// Database fields
	private SQLiteDatabase db;
	private MySQLiteOpenHelper dbHelper;


	public CategoryDAO(Context context) {
		dbHelper = new MySQLiteOpenHelper(context);
	}

	public void open() throws SQLException {
		if(db == null) {
			db = dbHelper.getWritableDatabase();
		}
	}

	public void close() {
		dbHelper.close();
	}

	public List<Category> getCategory(boolean isActive) {
		List<Category> rssList = new ArrayList<Category>();
		// Select All Query
		String selectQuery = 
				" SELECT  type, name, active" +
				" FROM " + TABLE_NAME +  
				" WHERE active = " + ( isActive == true ? "'1'" : "'0'")  ;
		log.d("VTT>>>getCategory selectQuery " + selectQuery);
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				Category rssSource = new Category();
				rssSource.type = cursor.getString(0);
				rssSource.name = cursor.getString(1);
				rssSource.active = cursor.getString(2);
				
				rssList.add(rssSource);
			} while (cursor.moveToNext());
		}

		return rssList;
	}
}
